function ajax (url){
  return new Promise(function(resolve, reject){
    var xhr = new XMLHttpRequest()
    xhr.open('GET', url)
    xhr.responseType = 'json'
    xhr.onload = function() { // 请求完成后
      if (this.status === 200) {
        resolve(this.response)
      }else{
        reject(new Error(this.statusText))
      }
    }
    xhr.send() // 开始执行
  })
}

var promise = ajax('./api/users.json')

promise
  .then(
  function onFulfilled (value) {
    console.log('onFulfilled',value)
  })
  .catch(
    function onRejected(error){
      console.log('onRejected', error)
    }
  )

window.addEventListener('unhandledrejection',event =>{
  const {reason, promise} = event

  console.log(reason, promise)

  event.preventDefault()
},false)
